/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormPessoa.java
 *
 * Created on 17/03/2011, 19:39:26
 */

package Interface;

import Controle.ControlaPessoa;
import Interface.Combos.Itens;
import Modelo.ConexaoBD;
import Modelo.Pessoa;
import classes.Popup;
import classes.Validacao;
import java.awt.event.KeyEvent;
import java.sql.*;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Form para cadastro de pessoas
 * @author Jonas Gualberto Diel [jonasdiel@gmail.com]
 */
public class FormPessoa extends javax.swing.JInternalFrame {

    public static Connection con;
    public static Statement stmt;
    private static String FUNCTION;

    private int personId; //Chave primária da tabela

    /**
     * Construtor
     * Cria o formulário de pessoa
     */
    public FormPessoa() {
        abreConexao();
        initComponents();
        preparaComboBox();
    }

    //Abre uma conexão com o banco de dados
    private void abreConexao() {
        try {
            con = ConexaoBD.getInstance().getConnection();
            stmt = con.createStatement();
        } catch (Exception e) {
            System.out.print(e);
        }
    }

    public void preparaComboBox()
    {
        // Populando as combos
        Combos cmb = new Combos("basCity");
        Map<String, Itens> m = cmb.getMap();

        // limpa Combo
        cityId.removeAllItems();
        // Preenche a primeira opcao
        cityId.addItem("::Selecione::");
        Set<String> chaves = m.keySet();
        
        Iterator<String> it = chaves.iterator();
        while (it.hasNext()) {
        String chave = it.next();
        cityId.addItem(chave);
        }
    }

    public void popularCampos(int personId)
    {
        ControlaPessoa controle = new ControlaPessoa();
        Pessoa pessoa = controle.obterPessoa(personId);

        //preencher cada campo com respectivo valor do objeto
        this.personId = pessoa.getPersonId();

        name.setText(pessoa.getName());
        shortName.setText(pessoa.getShortName());
        email.setText(pessoa.getEmail());
        emailAlternative.setText(pessoa.getEmailAlternative());

        //Cria objeto para o combobox
        Combos cmb = new Combos("basCity", "cityId", pessoa.getCityId());
        Map<String, Itens> m = cmb.getMap();
        Set<String> chaves = m.keySet();
        Iterator<String> it = chaves.iterator();
        while (it.hasNext()) {
        String codigoCidade = it.next();
        cityId.setSelectedItem(codigoCidade); //Popula o combobox
        }
        
        location.setText(pessoa.getLocation());
        number.setText(String.valueOf(pessoa.getNumber()));
        complement.setText(pessoa.getComplement());
        neighborhood.setText(pessoa.getNeighborhood());
        zipCode.setText(pessoa.getZipCode());

    }

    public void limparCampos()
    {
        name.setText("");
        shortName.setText("");
        email.setText("");
        emailAlternative.setText("");
        cityId.setSelectedIndex(0);
        location.setText("");
        number.setText("");
        complement.setText("");
        neighborhood.setText("");
        zipCode.setText("");
    }

    /**
     * Método para validar os campos do formulário
     * @return
     */
    private boolean validarCampos()
    {
        validaNome.setText("");
        validaNumero.setText("");
        validaCidade.setText("");

        boolean retorno = true;
        if( ! Validacao.validarObrigatorio(name.getText()) ) //Nome
        {
            validaNome.setText(Validacao.ERRO_CAMPO_OBRIGATORIO);
            retorno = false;
        }

        if( ! Validacao.validarInteiro(number.getText(), false) ) //Numero
        {
            validaNumero.setText(Validacao.ERRO_VALOR_INVALIDO);
            retorno = false;
        }

        if( cityId.getSelectedIndex() == 0 )
        {
            validaCidade.setText(Validacao.ERRO_CAMPO_OBRIGATORIO);
            retorno = false;
        }
        
        return retorno;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        aba = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        labelName = new javax.swing.JLabel();
        name = new javax.swing.JTextField();
        LabelShortName = new javax.swing.JLabel();
        shortName = new javax.swing.JTextField();
        email = new javax.swing.JTextField();
        labelEmail = new javax.swing.JLabel();
        emailAlternative = new javax.swing.JTextField();
        labelEmailAlternative = new javax.swing.JLabel();
        validaNome = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        labelLocation = new javax.swing.JLabel();
        location = new javax.swing.JTextField();
        labelNumber = new javax.swing.JLabel();
        number = new javax.swing.JTextField();
        labelComplement = new javax.swing.JLabel();
        complement = new javax.swing.JTextField();
        labelNeighborhood = new javax.swing.JLabel();
        neighborhood = new javax.swing.JTextField();
        labelCityId = new javax.swing.JLabel();
        cityId = new javax.swing.JComboBox();
        zipCode = new javax.swing.JTextField();
        labelZipCode = new javax.swing.JLabel();
        validaNumero = new javax.swing.JLabel();
        validaCidade = new javax.swing.JLabel();
        btnLimpar = new javax.swing.JButton();
        btnSalvar = new javax.swing.JButton();
        jInternalFrame1 = new javax.swing.JInternalFrame();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        jPanel4 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        jTextField10 = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jTextField11 = new javax.swing.JTextField();
        jTextField12 = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jTextField13 = new javax.swing.JTextField();
        jTextField14 = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jLabel16 = new javax.swing.JLabel();
        jTextField15 = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        jTextField16 = new javax.swing.JTextField();
        jLabel18 = new javax.swing.JLabel();
        jTextField17 = new javax.swing.JTextField();
        jLabel19 = new javax.swing.JLabel();
        jTextField18 = new javax.swing.JTextField();
        jLabel20 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createEtchedBorder());
        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setIconifiable(true);
        setTitle("Cadastrar Pessoa");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/imagens/menu/user.png"))); // NOI18N
        setName("FormPessoa"); // NOI18N
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        labelName.setText("Nome:");

        name.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nameActionPerformed(evt);
            }
        });
        name.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                nameKeyPressed(evt);
            }
        });

        LabelShortName.setText("Apelido:");

        shortName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                shortNameKeyPressed(evt);
            }
        });

        email.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                emailKeyPressed(evt);
            }
        });

        labelEmail.setText("E-mail:");

        emailAlternative.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                emailAlternativeKeyPressed(evt);
            }
        });

        labelEmailAlternative.setText("E-mail alternativo:");

        validaNome.setForeground(new java.awt.Color(255, 0, 0));
        validaNome.setVerticalTextPosition(javax.swing.SwingConstants.TOP);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(labelName)
                        .addGap(74, 74, 74)
                        .addComponent(name, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(validaNome, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(LabelShortName)
                                    .addComponent(labelEmail))
                                .addGap(66, 66, 66))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(labelEmailAlternative)
                                .addGap(18, 18, 18)))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(emailAlternative)
                            .addComponent(shortName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(email, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(validaNome, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(labelName)
                        .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LabelShortName)
                    .addComponent(shortName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelEmail)
                    .addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(emailAlternative, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelEmailAlternative))
                .addContainerGap(94, Short.MAX_VALUE))
        );

        aba.addTab("Dados pessoais", jPanel2);

        labelLocation.setText("Logradouro:");

        location.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                locationActionPerformed(evt);
            }
        });
        location.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                locationKeyPressed(evt);
            }
        });

        labelNumber.setText("Número:");

        number.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                numberKeyPressed(evt);
            }
        });

        labelComplement.setText("Complemento:");

        complement.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                complementKeyPressed(evt);
            }
        });

        labelNeighborhood.setText("Bairro:");

        neighborhood.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                neighborhoodActionPerformed(evt);
            }
        });
        neighborhood.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                neighborhoodKeyPressed(evt);
            }
        });

        labelCityId.setText("Cidade:");

        cityId.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cityId.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                cityIdKeyPressed(evt);
            }
        });

        zipCode.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                zipCodeKeyPressed(evt);
            }
        });

        labelZipCode.setText("CEP:");

        validaNumero.setForeground(new java.awt.Color(255, 0, 0));

        validaCidade.setForeground(new java.awt.Color(255, 0, 0));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelLocation)
                            .addComponent(labelNumber)
                            .addComponent(labelComplement)
                            .addComponent(labelNeighborhood))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(complement, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
                            .addComponent(neighborhood, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                                    .addComponent(number, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(validaNumero, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addComponent(location, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 298, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(149, 149, 149))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(labelZipCode)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 84, Short.MAX_VALUE)
                                .addComponent(zipCode, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(labelCityId)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 70, Short.MAX_VALUE)
                                .addComponent(cityId, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(10, 10, 10)
                        .addComponent(validaCidade, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(122, 122, 122))))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelCityId)
                    .addComponent(cityId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(validaCidade, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(zipCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelZipCode))
                .addGap(9, 9, 9)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelLocation)
                    .addComponent(location, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNumber)
                    .addComponent(number, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(validaNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelComplement)
                    .addComponent(complement, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNeighborhood)
                    .addComponent(neighborhood, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(54, Short.MAX_VALUE))
        );

        aba.addTab("Endereço", jPanel3);

        btnLimpar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/imagens/draw_eraser.png"))); // NOI18N
        btnLimpar.setText("Limpar");
        btnLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimparActionPerformed(evt);
            }
        });

        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/imagens/save_as.png"))); // NOI18N
        btnSalvar.setText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        jInternalFrame1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jInternalFrame1.setClosable(true);
        jInternalFrame1.setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        jInternalFrame1.setTitle("Cadastrar Pessoa");
        jInternalFrame1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jInternalFrame1.setName("FormPessoa"); // NOI18N

        jLabel11.setText("Nome:");

        jTextField10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField10ActionPerformed(evt);
            }
        });

        jLabel12.setText("Apelido:");

        jLabel13.setText("E-mail:");

        jLabel14.setText("URL:");

        jLabel15.setText("E-mail alternativo:");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addComponent(jLabel12)
                            .addComponent(jLabel13)
                            .addComponent(jLabel14))
                        .addGap(66, 66, 66))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel15)
                        .addGap(18, 18, 18)))
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jTextField14, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField13)
                    .addComponent(jTextField11, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField12, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
                    .addComponent(jTextField10, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap(97, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(jTextField10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(jTextField11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(jTextField12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField13, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel15))
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField14, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14))
                .addContainerGap(56, Short.MAX_VALUE))
        );

        jTabbedPane2.addTab("Dados pessoais", jPanel4);

        jLabel16.setText("Logradouro:");

        jLabel17.setText("Número:");

        jLabel18.setText("Complemento:");

        jLabel19.setText("Bairro:");

        jLabel20.setText("Cidade:");

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel16)
                    .addComponent(jLabel17)
                    .addComponent(jLabel18)
                    .addComponent(jLabel19)
                    .addComponent(jLabel20))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jTextField16, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jTextField15, javax.swing.GroupLayout.PREFERRED_SIZE, 298, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jTextField17, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
                        .addComponent(jTextField18, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(84, 84, 84))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel20)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel16)
                    .addComponent(jTextField15, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel17)
                    .addComponent(jTextField16, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel18)
                    .addComponent(jTextField17, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel19)
                    .addComponent(jTextField18, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(83, Short.MAX_VALUE))
        );

        jTabbedPane2.addTab("Endereço", jPanel5);

        jButton3.setText("Limpar");

        jButton4.setText("Salvar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
        jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
        jInternalFrame1Layout.setHorizontalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jInternalFrame1Layout.createSequentialGroup()
                        .addComponent(jButton4)
                        .addGap(18, 18, 18)
                        .addComponent(jButton3))
                    .addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 536, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jInternalFrame1Layout.setVerticalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jInternalFrame1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 266, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton4)
                    .addComponent(jButton3))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(aba, javax.swing.GroupLayout.PREFERRED_SIZE, 601, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnSalvar)
                        .addGap(18, 18, 18)
                        .addComponent(btnLimpar)))
                .addContainerGap(45, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 337, Short.MAX_VALUE)
                    .addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 338, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(aba, javax.swing.GroupLayout.PREFERRED_SIZE, 266, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalvar)
                    .addComponent(btnLimpar))
                .addGap(46, 46, 46))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 199, Short.MAX_VALUE)
                    .addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 198, Short.MAX_VALUE)))
        );

        getAccessibleContext().setAccessibleName("");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void nameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_nameActionPerformed

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed

        //Obtem valor da combo
        Combos cmb = new Combos("basCity");
        Map<String, Itens> m = cmb.getMap();
        Itens i = m.get(cityId.getSelectedItem());

        //Validação dos campos                
        if( this.validarCampos() )
        {
            Pessoa pessoa = new Pessoa();
            System.out.println(this.personId);

            pessoa.setPersonId(this.personId);
            pessoa.setName(name.getText());
            pessoa.setShortName(shortName.getText());
            pessoa.setEmail(email.getText());
            pessoa.setEmailAlternative(emailAlternative.getText());
            pessoa.setCityId(i.getCodigo());
            pessoa.setLocation(location.getText());
            if( number.getText().length() > 0 )
            {
                pessoa.setNumber(Integer.parseInt(number.getText()));
            }
            pessoa.setComplement(complement.getText());
            pessoa.setNeighborhood(neighborhood.getText());
            pessoa.setZipCode(zipCode.getText());

            Popup info = Popup.obterInstancia();
            ControlaPessoa ctlPessoa = new ControlaPessoa(pessoa, null);

            if ( getFunction() == Form.FUNCTION_INSERT ) //INSERÇÃO
            {
                if (ctlPessoa.inserirPessoa())
                {
                    FormPrincipal.formPessoaBusca.autalizarTabela(); //Atualizar a tabela do  formmulário de busca
                    if ( info.pedirConfirmacao("Registro inserido com sucesso! Adicionar novo registro?") )
                    {
                        //Prepara para inserir novo registro
                        FormPrincipal.formPessoa.setFunction(Form.FUNCTION_INSERT); //Define a função de inserção
                        FormPrincipal.formPessoa.limparCampos(); //Limpa o formulário para inserir novo registro
                        FormPrincipal.formPessoa.setTitle("Inserir Pessoa"); //Define o título
                        FormPrincipal.formPessoa.setVisible(true); //Abre a janela
                    }
                    else
                    {
                        //Fecha a tela
                        this.setVisible(false);
                    }
                }
                else
                {
                    //ERRO
                    info.exibirMensagem("Erro ao inserir registro", 'e');
                }
            }
            else
            {
                if ( getFunction() == Form.FUNCTION_UPDATE ) //EDIÇÃO
                {
                    if (ctlPessoa.editarPessoa())
                    {
                        FormPrincipal.formPessoaBusca.autalizarTabela(); //Atualizar a tabela do  formmulário de busca                        
                        info.exibirMensagem("Edição realizada com sucesso!", 'i');
                        FormPrincipal.formPessoa.setVisible(false); //Fecha a janela
                    }
                }
            }

        }
    }//GEN-LAST:event_btnSalvarActionPerformed

     /**
     * @return the FUNCTION
     */
    public static String getFunction() {
        return FUNCTION;
    }

    /**
     * @param aFUNCTION the FUNCTION to set
     */
    public static void setFunction(String aFUNCTION) {
        //Ao alterar a função sempre exibe a primeira aba
        aba.setSelectedIndex(0);
        FUNCTION = aFUNCTION;
    }

    private void jTextField10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField10ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField10ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton4ActionPerformed

    private void neighborhoodActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_neighborhoodActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_neighborhoodActionPerformed

    private void btnLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimparActionPerformed
        limparCampos();
    }//GEN-LAST:event_btnLimparActionPerformed

    private void nameKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nameKeyPressed
       String tecla = KeyEvent.getKeyText(evt.getKeyCode());
       if ( tecla.equalsIgnoreCase("Enter") )
       {
           shortName.requestFocus();
       }
    }//GEN-LAST:event_nameKeyPressed

    private void shortNameKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_shortNameKeyPressed
        String tecla = KeyEvent.getKeyText(evt.getKeyCode());
       if ( tecla.equalsIgnoreCase("Enter") )
       {
           email.requestFocus();
       }
    }//GEN-LAST:event_shortNameKeyPressed

    private void emailKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_emailKeyPressed
        String tecla = KeyEvent.getKeyText(evt.getKeyCode());
       if ( tecla.equalsIgnoreCase("Enter") )
       {
           emailAlternative.requestFocus();
       }
    }//GEN-LAST:event_emailKeyPressed

    private void emailAlternativeKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_emailAlternativeKeyPressed
       String tecla = KeyEvent.getKeyText(evt.getKeyCode());
       if ( tecla.equalsIgnoreCase("Enter") )
       {
           aba.setSelectedIndex(1);
           cityId.setFocusable(true);
       }
    }//GEN-LAST:event_emailAlternativeKeyPressed

    private void cityIdKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cityIdKeyPressed
       String tecla = KeyEvent.getKeyText(evt.getKeyCode());
       if ( tecla.equalsIgnoreCase("Enter") )
       {
           zipCode.requestFocus();
       }
    }//GEN-LAST:event_cityIdKeyPressed

    private void zipCodeKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_zipCodeKeyPressed
        String tecla = KeyEvent.getKeyText(evt.getKeyCode());
       if ( tecla.equalsIgnoreCase("Enter") )
       {
           location.requestFocus();
       }
    }//GEN-LAST:event_zipCodeKeyPressed

    private void locationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_locationActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_locationActionPerformed

    private void locationKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_locationKeyPressed
       String tecla = KeyEvent.getKeyText(evt.getKeyCode());
       if ( tecla.equalsIgnoreCase("Enter") )
       {
           number.requestFocus();
       }
    }//GEN-LAST:event_locationKeyPressed

    private void numberKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_numberKeyPressed
         String tecla = KeyEvent.getKeyText(evt.getKeyCode());
       if ( tecla.equalsIgnoreCase("Enter") )
       {
           complement.requestFocus();
       }
    }//GEN-LAST:event_numberKeyPressed

    private void complementKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_complementKeyPressed
         String tecla = KeyEvent.getKeyText(evt.getKeyCode());
       if ( tecla.equalsIgnoreCase("Enter") )
       {
           neighborhood.requestFocus();
       }
    }//GEN-LAST:event_complementKeyPressed

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        String tecla = KeyEvent.getKeyText(evt.getKeyCode());
       if ( tecla.equalsIgnoreCase("Enter") )
       {
           btnSalvarActionPerformed(null);
       }
    }//GEN-LAST:event_formKeyPressed

    private void neighborhoodKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_neighborhoodKeyPressed
        String tecla = KeyEvent.getKeyText(evt.getKeyCode());
       if ( tecla.equalsIgnoreCase("Enter") )
       {
           this.btnSalvarActionPerformed(null);
       }
    }//GEN-LAST:event_neighborhoodKeyPressed


    @Override
    public void setDefaultCloseOperation(int operation) {

        
        super.setDefaultCloseOperation(operation);
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel LabelShortName;
    public static javax.swing.JTabbedPane aba;
    private javax.swing.JButton btnLimpar;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JComboBox cityId;
    private javax.swing.JTextField complement;
    private javax.swing.JTextField email;
    private javax.swing.JTextField emailAlternative;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JTextField jTextField10;
    private javax.swing.JTextField jTextField11;
    private javax.swing.JTextField jTextField12;
    private javax.swing.JTextField jTextField13;
    private javax.swing.JTextField jTextField14;
    private javax.swing.JTextField jTextField15;
    private javax.swing.JTextField jTextField16;
    private javax.swing.JTextField jTextField17;
    private javax.swing.JTextField jTextField18;
    private javax.swing.JLabel labelCityId;
    private javax.swing.JLabel labelComplement;
    private javax.swing.JLabel labelEmail;
    private javax.swing.JLabel labelEmailAlternative;
    private javax.swing.JLabel labelLocation;
    private javax.swing.JLabel labelName;
    private javax.swing.JLabel labelNeighborhood;
    private javax.swing.JLabel labelNumber;
    private javax.swing.JLabel labelZipCode;
    private javax.swing.JTextField location;
    private javax.swing.JTextField name;
    private javax.swing.JTextField neighborhood;
    private javax.swing.JTextField number;
    private javax.swing.JTextField shortName;
    private javax.swing.JLabel validaCidade;
    private javax.swing.JLabel validaNome;
    private javax.swing.JLabel validaNumero;
    private javax.swing.JTextField zipCode;
    // End of variables declaration//GEN-END:variables

}
